﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace mrp16062010.Serialize
{
    class BinarySerialization
    {
        public static byte[] BinarySerialize(Object obj)
        {
            byte[] serializedObject;
            MemoryStream ms = new MemoryStream();
            BinaryFormatter b = new BinaryFormatter();
            b.Serialize(ms, obj);
            ms.Seek(0, 0);
            serializedObject = ms.ToArray();
            ms.Close();
            return serializedObject;
        }

        public static T BinaryDeSerialize<T>(byte[] serializedObject)
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(serializedObject, 0, serializedObject.Length);
            ms.Seek(0, 0);
            BinaryFormatter b = new BinaryFormatter();
            Object obj = b.Deserialize(ms);
            ms.Close();
            return (T)obj;
        }
    }
}
